package com.avic.map.service.impl;

import com.avic.map.common.model.MapCommonModel;
import com.avic.map.dal.condition.PageQueryMapShipCondition;
import com.avic.map.dal.model.MapShipResource;
import com.avic.map.service.MapShipResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 应急监控船舶接口service实现类
 * @Author huangsheng
 * @Date 2018/10/1 15:56
 */
@Service
@Slf4j
public class MapShipResourceServiceImpl extends AbstractServiceImpl implements MapShipResourceService {

    /**
     * @param mapCommonModel
     * @see MapShipResourceService#createMapShipResource(MapCommonModel)
     */
    @Override
    public void createMapShipResource(MapCommonModel mapCommonModel) {
        try {
            MapShipResource mapShipResource = new MapShipResource();
            mapShipResource.setShipNumber(mapCommonModel.getModelId());
            mapShipResource.setShipName(mapCommonModel.getModelName());

            int insertMapShipResource = mapShipResourceMapper.insertMapShipResource(mapShipResource);
            log.info("新增船舶数据结果 {}", insertMapShipResource);
        } catch (DuplicateKeyException de) {
            log.error("船舶数据已经存在，重复创建");
        } catch (Exception e) {
            log.error("创建地图船舶数据发生错误{}", e.getMessage());
        }
    }

    /**
     * @param shipNumber
     * @see MapShipResourceService#deleteMapShipResource(String)
     */
    @Override
    public void deleteMapShipResource(String shipNumber) {
        try {
            log.info("删除船舶数据{}", shipNumber);
            int deleteByDataNumber = mapShipResourceMapper.deleteByShipNumber(shipNumber);
            log.info("删除船舶数据结果 {}", deleteByDataNumber);
        } catch (Exception e) {
            log.error("删除地图应急船舶数据发生错误{}", e.getMessage());
        }
    }

    /**
     * @param pageQueryMapShipCondition
     * @return
     * @see MapShipResourceService#mapShipList(PageQueryMapShipCondition)
     */
    @Override
    public List<MapShipResource> mapShipList(PageQueryMapShipCondition pageQueryMapShipCondition) {
        return mapShipResourceMapper.mapShipList(pageQueryMapShipCondition);
    }

    /**
     * @see MapShipResourceService#deleteAll()
     */
    @Override
    public void deleteAll() {
        try {
            mapShipResourceMapper.deleteAll();
        } catch (Exception e) {
            log.error("删除所有地图数据发生错误 {}", e.getMessage());
        }
    }

    @Override
    public int mapShipListTotal(PageQueryMapShipCondition pageQueryMapShipCondition) {
        return mapShipResourceMapper.mapShipListTotal(pageQueryMapShipCondition);
    }

    @Override
    public void updateShipName(String shipName, String shipNumber) {
        try {
            mapShipResourceMapper.updateShipName(shipName, shipNumber);
        } catch (Exception e) {
            log.error("更新船舶名称发生错误 {}", e.getMessage());
        }
    }
}
